package com.service.impl;

import com.constant.BussinessResultCodeEnum;
import com.dao.CustomerMapper;
import com.dao.DeviceRepairApplyMapper;
import com.dao.OrdersMapper;
import com.dao.VenderDeviceMapper;
import com.exception.LoginTimeOutException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.Customer;
import com.model.DeviceRepairApply;
import com.model.Orders;
import com.model.VenderDevice;
import com.page.GenericPage;
import com.po.DeviceRepairApplyPO;
import com.po.DeviceRepairApplyQueryConditionPO;
import com.result.Result;
import com.service.DeviceRepairApplyService;
import com.utils.CurrentLoginUserAgent;
import com.utils.SessionUtil;
import com.vo.DeviceRepairApplyVO;
import com.vo.LoginData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.testng.collections.Lists;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chihaojie
 * @Date: 2019/6/18 8:34
 * @Version 1.0
 * @Note
 */
@Service
public class DeviceRepairApplyServiceImpl  implements DeviceRepairApplyService {

    @Autowired
    private DeviceRepairApplyMapper deviceRepairApplyMapper;

    @Autowired
    private OrdersMapper   ordersMapper;


    @Autowired
    private VenderDeviceMapper  deviceMapper;

    @Autowired
    private CustomerMapper  customerMapper;


    @Override
    public Result<GenericPage<DeviceRepairApplyVO>> getDeviceRepairApplyPage(DeviceRepairApplyQueryConditionPO queryConditionPO, int pageNo, int pageSize) {
        Assert.notNull(queryConditionPO,"查询参数不能为空");
        Long businessId = CurrentLoginUserAgent.getBusinessId();
        List<DeviceRepairApplyVO>  voList= Lists.newArrayList();
        //构造分页查询条件
        DeviceRepairApply condition=new DeviceRepairApply();
        BeanUtils.copyProperties(queryConditionPO,condition);
        condition.setAgent(businessId);
        PageHelper.startPage(pageNo,pageSize);
        List<DeviceRepairApply> applyList=   deviceRepairApplyMapper.selectDeviceRepairApplyListByConditon(condition);
        if(!ObjectUtils.isEmpty(applyList)){
             voList = applyList.stream().map((e) -> {
                DeviceRepairApplyVO vo = new DeviceRepairApplyVO();
                BeanUtils.copyProperties(e, vo);
                return vo;
            }).collect(Collectors.toList());
        }
        PageInfo<DeviceRepairApplyVO> pageInfo = new PageInfo<>(voList);
        GenericPage<DeviceRepairApplyVO> genericPage = new GenericPage<>(pageSize, pageInfo.getTotal(), voList);
        return new Result<GenericPage<DeviceRepairApplyVO>>().ok(genericPage);
    }

    @Override
    public Result<DeviceRepairApplyVO> getDeviceRepairApplyDetail(Long id) {
        DeviceRepairApplyVO vo=new DeviceRepairApplyVO();
        DeviceRepairApply deviceRepairApply = deviceRepairApplyMapper.selectByPrimaryKey(id);
        if(!ObjectUtils.isEmpty(deviceRepairApply)){
            BeanUtils.copyProperties(deviceRepairApply,vo);
        }
        return new Result<DeviceRepairApplyVO>().ok(vo);
    }


    @Override
    public Result<Boolean> repair(DeviceRepairApplyPO repairApplyPO) {
        Result<Boolean>  result =new Result<Boolean>();
        if(ObjectUtils.isEmpty(repairApplyPO)
                || ObjectUtils.isEmpty(repairApplyPO.getChannel())
                || ObjectUtils.isEmpty(repairApplyPO.getDeviceId())
                || ObjectUtils.isEmpty(repairApplyPO.getOrderNum())
                || ObjectUtils.isEmpty(repairApplyPO.getComplainantOpenId())){
            return result.error(BussinessResultCodeEnum.PARAM_NOT_COMPLETE.resultCode,BussinessResultCodeEnum.PARAM_NOT_COMPLETE.resultMsg);
        }
        if(ObjectUtils.isEmpty(repairApplyPO.getTroubleDesc())){
            return result.error(BussinessResultCodeEnum.TROUBLE_DESC_NOT_NULL.resultCode,BussinessResultCodeEnum.TROUBLE_DESC_NOT_NULL.resultMsg);

        }
        //生成报修记录
        DeviceRepairApply  repairApply=new DeviceRepairApply();
        Date    now =new Date();
        //查询订单详情
        Orders order = ordersMapper.selectByOrderNumber(repairApplyPO.getOrderNum());
        if(ObjectUtils.isEmpty(order)){
            return result.error(BussinessResultCodeEnum.NO_SUCH_ORDER.resultCode,BussinessResultCodeEnum.NO_SUCH_ORDER.resultMsg);
        }
        VenderDevice device = deviceMapper.selectByPrimaryKey(order.getDeviceId());
        Customer customer=null;
        if(repairApplyPO.getChannel().equals(1)){
             customer = customerMapper.selectCustomerInfoByWXOpenId(repairApplyPO.getComplainantOpenId());
            //报修人信息
            repairApply.setComplainantOpenid(repairApplyPO.getComplainantOpenId());
            repairApply.setComplainant(customer.getWechatNickName());
        }
        repairApply.setChannel(repairApplyPO.getChannel());
        repairApply.setAgent(device.getAgent());
        repairApply.setAgentInfo("代理商");
        repairApply.setDeviceId(device.getId());
        repairApply.setDeviceSn(device.getDeviceSn());
        repairApply.setDeviceName(device.getDeviceName());
        repairApply.setHospitalId(order.getHospitalId());
        repairApply.setHospitalName(order.getHospitalName());
        repairApply.setBoxType(order.getBoxType());
        repairApply.setBoxNum(order.getBoxNum());
        repairApply.setOrderId(order.getId());
        repairApply.setOrderNum(order.getOrderNum());
        //地址信息
        repairApply.setProvinceName(device.getProvinceName());
        repairApply.setCityName(device.getCityName());
        repairApply.setCountyName(device.getCountyName());
        repairApply.setStreetName(device.getStreetName());
        repairApply.setDetailAddress(device.getDetailAddress());
        //故障描述
        repairApply.setTroubleShootStatus(1);
        repairApply.setTroubleDesc(repairApplyPO.getTroubleDesc().trim());
        repairApply.setTroubleImg(repairApplyPO.getTroubleImg());
        repairApply.setTroubleVideo(repairApplyPO.getTroubleVideo());
        repairApply.setComplainantTime(now);
        repairApply.setCreateTime(now);
        deviceRepairApplyMapper.insert(repairApply);
        return result.ok(true);
    }

    @Override
    public Result<Boolean> troubleShootStatus(Long id, Integer troubleShootStatus) {
        Assert.notNull(troubleShootStatus,"故障状态参数不能为空");
        DeviceRepairApply deviceRepairApply = deviceRepairApplyMapper.selectByPrimaryKey(id);
        if(ObjectUtils.isEmpty(deviceRepairApply)){
            return  new Result<>().error("记录不存在");
        }
        deviceRepairApply.setTroubleShootStatus(troubleShootStatus);
        deviceRepairApplyMapper.updateByPrimaryKey(deviceRepairApply);
        return new Result<Boolean>().ok(true);
    }
}
